/**
 * Project: Useful Programs <br>
 * Package: mathematics.probability
 */
package org.leumasjaffe.math.probability;

import java.util.Random;

import org.eclipse.jdt.annotation.NonNullByDefault;

import flanagan.analysis.Stat;

/**
 * Class .NegativeBinomialRV in file NegativeBinomialRV.java <br>
 * 
 * @author leumasjaffe
 * Created on: Aug 21, 2012
 * 
 */
@NonNullByDefault
public class NegativeBinomialRV extends DiscreteRV {

	private final int k;
	private final double p;
	private final double q;
	
	/**
	 * 
	 */
	public NegativeBinomialRV(int k, double p) {
		super();
		this.k = k;
		this.p = p;
		this.q = 1-p;
	}

	/**
	 * @param r
	 */
	public NegativeBinomialRV(Random r, int k, double p) {
		super(r);
		this.k = k;
		this.p = p;
		this.q = 1-p;
	}

	/* (non-Javadoc)
	 * @see mathematics.probability.AbstractRV#getEV()
	 */
	@Override
	public double getEV() {
		return this.p*this.k/this.q;
	}

	/* (non-Javadoc)
	 * @see mathematics.probability.AbstractRV#getVar()
	 */
	@Override
	public double getVar() {
		return getEV()/this.q;
	}

	/* (non-Javadoc)
	 * @see mathematics.probability.AbstractRV#getP(double)
	 */
	@Override
	public double getP(int n) {
		return ((n-1)/this.k)*Math.pow(this.q, n-this.k)*Math.pow(this.p, this.k);
	}

	/* (non-Javadoc)
	 * @see mathematics.probability.AbstractRV#calcX(double)
	 */
	@Override
	public double calcX(double p) {
		throw new RuntimeException("Not yet implemented");
	}

	/* (non-Javadoc)
	 * @see mathematics.probability.AbstractRV#getF(double)
	 */
	@Override
	public double getF(int x) {
		int n = (int) x;
		return 1-Stat.incompleteBeta(this.k+1, n-this.k, this.p);
	}

}
